package cn.itrip.biz.controller;

import cn.itrip.beans.dtos.Dto;
import cn.itrip.beans.pojo.ItripAreaDic;
import cn.itrip.beans.pojo.ItripImage;
import cn.itrip.beans.pojo.ItripLabelDic;
import cn.itrip.beans.vo.ItripAreaDicVO;
import cn.itrip.beans.vo.ItripImageVO;
import cn.itrip.beans.vo.ItripLabelDicVO;
import cn.itrip.beans.vo.hotel.HotelVideoDescVO;
import cn.itrip.beans.vo.hotel.ItripSearchDetailsHotelVO;
import cn.itrip.beans.vo.hotel.ItripSearchFacilitiesHotelVO;
import cn.itrip.beans.vo.hotel.ItripSearchPolicyHotelVO;
import cn.itrip.biz.service.itripAreaDic.ItripAreaDicService;
import cn.itrip.biz.service.itripHotel.ItripHotelService;
import cn.itrip.biz.service.itripImage.ItripImageService;
import cn.itrip.biz.service.itripLabelDic.ItripLabelDicService;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.EmptyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/hotel")
public class HotelController {
    @Resource
    private ItripAreaDicService itripAreaDicService;
    @Resource
    private ItripImageService itripImageService;
    @Resource
    private ItripHotelService itripHotelService;
    @Resource
    private ItripLabelDicService itripLabelDicService;

    //查询国内外热门城市
    @RequestMapping(value = "/queryhotcity/{type}", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto<ItripAreaDicVO> queryHotCity(@PathVariable Integer type) throws Exception{
        if(type == null || type == 0){
            return DtoUtil.returnFail("type不能为空","10201");
        }
        try {
            Map<String,Object> param = new HashMap<String, Object>();
            param.put("isHot", 1);
            param.put("isChina", type);
            List<ItripAreaDic> result = itripAreaDicService.getItripAreaDicListByMap(param);
            List<ItripAreaDicVO> output = new ArrayList<ItripAreaDicVO>();
            if(EmptyUtils.isNotEmpty(result)){
                ItripAreaDicVO itripAreaDicVO = null;
                for(ItripAreaDic itripAreaDic:result){
                    itripAreaDicVO = new ItripAreaDicVO();
                    BeanUtils.copyProperties(itripAreaDic,itripAreaDicVO);
                    output.add(itripAreaDicVO);
                }
            }
            return DtoUtil.returnDataSuccess(output);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("系统异常，获取失败","10202");
        }
    }

    //查询某个城市下的商圈
    @RequestMapping(value = "/querytradearea/{cityId}", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto<ItripAreaDicVO> queryTradeArea(@PathVariable Integer cityId) throws Exception{
        if(cityId == null || cityId == 0){
            return DtoUtil.returnFail("cityId不能为空","10203");
        }
        try {
            Map<String,Object> param = new HashMap<String, Object>();
            param.put("isTradingArea", 1);
            param.put("parent", cityId);
            List<ItripAreaDic> result = itripAreaDicService.getItripAreaDicListByMap(param);
            List<ItripAreaDicVO> output = new ArrayList<ItripAreaDicVO>();
            if(EmptyUtils.isNotEmpty(result)){
                ItripAreaDicVO itripAreaDicVO = null;
                for(ItripAreaDic itripAreaDic:result){
                    itripAreaDicVO = new ItripAreaDicVO();
                    BeanUtils.copyProperties(itripAreaDic,itripAreaDicVO);
                    output.add(itripAreaDicVO);
                }
            }
            return DtoUtil.returnDataSuccess(output);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("系统异常，获取失败","10204");
        }
    }

    //查询某个酒店的图片
    @RequestMapping(value = "/getimg/{targetId}", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto<ItripImageVO> getImgForHotel(@PathVariable String targetId) throws Exception{
        if(EmptyUtils.isEmpty(targetId)){
            return DtoUtil.returnFail("酒店id不能为空","100213");
        }
        try {
            Map<String,Object> param = new HashMap<String,Object>();
            param.put("type","0");
            param.put("targetId",Long.valueOf(targetId));
            List<ItripImage> result = itripImageService.getItripImageListByMap(param);
            List<ItripImageVO>  output = new ArrayList<>();
            if(EmptyUtils.isNotEmpty(result)){
                ItripImageVO vo = null;
                for(ItripImage img:result){
                    vo = new ItripImageVO();
                    BeanUtils.copyProperties(img,vo);
                    output.add(vo);
                }
            }
            return DtoUtil.returnDataSuccess(output);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("系统异常，获取失败","100212");
        }
    }

    //查询酒店的视频描述详情
    @RequestMapping(value = "/getvideodesc/{hotelId}", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto<HotelVideoDescVO> getVideoDesc(@PathVariable String hotelId) throws Exception{
        if(EmptyUtils.isEmpty(hotelId)){
            return DtoUtil.returnFail("酒店id不能为空","100215");
        }
        try {
            HotelVideoDescVO result = itripHotelService.getVideoDescByHotel(Long.valueOf(hotelId));
            return DtoUtil.returnDataSuccess(result);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("获取酒店视频文字描述失败","100214");
        }
    }

    //查询酒店设施
    @RequestMapping(value = "/queryhotelfacilities/{id}", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto<ItripSearchFacilitiesHotelVO> queryHotelFacilities(@PathVariable Integer id) throws Exception{
        if(id == null || id == 0){
            return DtoUtil.returnFail("酒店id不能为空","10206");
        }
        try {
            ItripSearchFacilitiesHotelVO result = itripHotelService.getItripHotelFacilitiesById(Long.valueOf(id));
            return DtoUtil.returnDataSuccess(result);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("系统异常,获取失败","10207");
        }
    }

    //查询酒店特色和介绍
    @RequestMapping(value = "/queryhoteldetails/{id}", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto<ItripSearchDetailsHotelVO> queryHotelDetails(@PathVariable Integer id) throws Exception{
        if(id == null || id == 0){
            return DtoUtil.returnFail("酒店id不能为空","10210");
        }
        try {
            List<ItripSearchDetailsHotelVO> list = itripHotelService.getSearchDetailsByHotelId(Long.valueOf(id));
            return DtoUtil.returnDataSuccess(list);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("系统异常,获取失败","10211");
        }
    }

    //查询酒店特色列表
    @RequestMapping(value = "/queryhotelfeature", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto<ItripLabelDicVO> queryHotelFeature() throws Exception{
        try {
            Map<String,Object> param = new HashMap<String, Object>();
            param.put("parentId",16);
            List<ItripLabelDic> result = itripLabelDicService.getItripLabelDicListByMap(param);
            List<ItripLabelDicVO> output = new ArrayList<>();
            if(EmptyUtils.isNotEmpty(result)){
                ItripLabelDicVO labelDicVO = null;
                for(ItripLabelDic itripLabelDic:result){
                    labelDicVO = new ItripLabelDicVO();
                    BeanUtils.copyProperties(itripLabelDic,labelDicVO);
                    output.add(labelDicVO);
                }
            }
            return DtoUtil.returnDataSuccess(output);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("系统异常,获取失败","10205");
        }
    }

    //查询酒店政策
    @RequestMapping(value = "/queryhotelpolicy/{id}", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto<ItripSearchPolicyHotelVO> queryHotelPolicy(@PathVariable Integer id) throws Exception{
        if(id == null || id == 0){
            return DtoUtil.returnFail("酒店id不能为空","10208");
        }
        try {
            ItripSearchPolicyHotelVO result = itripHotelService.getHotelPolicyById(Long.valueOf(id));
            return DtoUtil.returnDataSuccess(result);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("系统异常,获取失败","10209");
        }
    }
}
